﻿using System;
using LocalizationUtils._3rdParty.AOTEasyAlphabetArabic;
using LocalizationUtils.Language;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace LocalizationUtils
{
    [DisallowMultipleComponent]
    public class DeleteLocalization : MonoBehaviour
    {
        private TextMeshProUGUI _textMeshProUGUI;

        public TextMeshProUGUI TextMeshProUGUI
        {
            get
            {
                if (_textMeshProUGUI == null)
                    _textMeshProUGUI = GetComponent<TextMeshProUGUI>();
                return _textMeshProUGUI;
            }

            set => _textMeshProUGUI = value;
        }

        public TextMeshPro TextMeshPro
        {
            get
            {
                if (_textMeshPro == null)
                    _textMeshPro = GetComponent<TextMeshPro>();
                return _textMeshPro;
            }
            set => _textMeshPro = value;
        }


        private TextMeshPro _textMeshPro;
        private Text _legaltext;

        public Text Legaltext
        {
            get
            {
                if (_legaltext == null)
                    _legaltext = GetComponent<Text>();
                return _legaltext;
            }
            set => _legaltext = value;
        }


        public bool _setOnAwake = true;
        public long _key;

        private string Text
        {
            set
            {
                if (TextMeshProUGUI)
                {
                    TextMeshProUGUI.text = value;
                }
                else if (TextMeshPro)
                {
                    TextMeshPro.text = value;
                }
                else if (Legaltext)
                {
                    Legaltext.text = value;
                }
            }
        }

        protected void Awake()
        {
            _textMeshProUGUI = GetComponent<TextMeshProUGUI>();
            if (_textMeshProUGUI == null)
            {
                _legaltext = GetComponent<Text>();
                if (_legaltext == null)
                {
                    _textMeshPro = GetComponent<TextMeshPro>();
                }
            }


            if (Application.isPlaying && _setOnAwake)
            {
                if (_key == 0)
                    return;

                Text = GetText(Lang.Get(_key));
            }
        }

        public void SetKey(long key)
        {
            _key = key;
            Text = GetText(Lang.Get(key));
        }

        public void SetKeyWithParams(long key, params object[] args)
        {
            _key = key;
            Text = GetText(Lang.GetFormat(key, args));
        }

        // args [pair]
        // k, v
        // 0： 直接用v
        // 1: getLang(v）
        //object[] args = { 0, "apple", 1, 1001, 0, "banana", 1, 1002, 0, "orange" };
        public void SetKeyWithParamsByType(long key, params object[] args)
        {
            object[] newArgs = new object[args.Length / 2];
            int newArgIndex = 0;

            for (int i = 0; i < args.Length; ++i)
            {
                if (i % 2 == 0)
                {
                    int paramsType = Convert.ToInt32(args[i]);
                    if (paramsType == 0)
                    {
                        newArgs[newArgIndex] = args[i + 1];
                    }
                    else if (paramsType == 1)
                    {
                        int tmpKey = Convert.ToInt32(args[i + 1]);
                        var text = Lang.Get(tmpKey);
                        newArgs[newArgIndex] = text;
                    }

                    newArgIndex++;
                }
            }

            SetKeyWithParams(key, newArgs);
        }

        public void SetParams(params object[] args)
        {
            SetKeyWithParams(_key, args);
        }

        public void SetParamsByType(params object[] args)
        {
            SetKeyWithParamsByType(_key, args);
        }


        private string GetText(string lang)
        {
            if (LocalizationManager.CurrSystemLanguage == LocalizationManager.CustomSystemLanguage.Arabic)
            {
                if (Legaltext)
                {
                    var aText = GetComponent<ArabicMultiLineText>();
                    if (aText != null)
                    {
                        aText.enabled = true;
                        aText.Text = lang;
                        aText.Start();
                        aText.Update();
                    }

                    return global::LocalizationUtils.Utils.LocalizationUtils.MirrorCharacters(
                        EasyArabicCore.CorrectString(lang));
                }

                if (TextMeshPro)
                    TextMeshPro.isRightToLeftText = true;
                if (TextMeshProUGUI)
                    TextMeshProUGUI.isRightToLeftText = true;
                return global::LocalizationUtils.Utils.LocalizationUtils.GetFixedText(lang);
            }

            if (GetComponent<ArabicMultiLineText>() != null)
            {
                GetComponent<ArabicMultiLineText>().enabled = false;
            }


            if (TextMeshPro)
                TextMeshPro.isRightToLeftText = false;
            if (TextMeshProUGUI)
                TextMeshProUGUI.isRightToLeftText = false;

            return lang;
        }
    }
}